package com.tx.framework.util;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 提供上传和下载excel方法
 * 
 * @author Administrator
 *
 */
public class FileUtil {

	/**
	 * 下载excel
	 * @param response
	 * @param workbook excel文件内容
	 * @param fileName 文件名不带后缀
	 */
	public static void downLoad(HttpServletResponse response,
			XSSFWorkbook workbook, String fileName) {
		if (workbook != null) {
			try {
				String allName = fileName + ".xlsx";
				// 下载后的excel名称可以为中文
				String downloadFileName = new String(allName.getBytes("UTF-8"),"iso-8859-1");
				String headStr = "attachment; filename=\"" + downloadFileName+ "\"";
				response.setContentType("application/x-download;charset=utf-8");
				response.setHeader("Content-Disposition", headStr);
				OutputStream out = response.getOutputStream();
				workbook.write(out);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
